﻿using System.Windows;
using System.Windows.Controls;

namespace LogInModule.Views
{
    public class SlideshowControl : ItemsControl
    {
        public static readonly DependencyProperty SelectedIndexProperty = DependencyProperty.Register("SelectedIndex", typeof(int), typeof(SlideshowControl), new PropertyMetadata(0));
        public int SelectedIndex
        {
            get { return (int)base.GetValue(SelectedIndexProperty); }
            set { base.SetValue(SelectedIndexProperty, value); }
        }

        public static readonly DependencyProperty SelectedItemProperty = DependencyProperty.Register("SelectedItem", typeof(object), typeof(SlideshowControl), new PropertyMetadata(null));
        public object SelectedItem
        {
            get { return base.GetValue(SelectedItemProperty); }
            set { base.SetValue(SelectedItemProperty, value); }
        }

        public static readonly DependencyProperty TransitionStyleProperty = DependencyProperty.Register("TransitionStyle", typeof(Style), typeof(SlideshowControl), new PropertyMetadata(null));
        public Style TransitionStyle
        {
            get { return (Style)GetValue(TransitionStyleProperty); }
            set { SetValue(TransitionStyleProperty, value); }
        }

        public static readonly DependencyProperty InCarouselModeProperty = DependencyProperty.Register("InCarouselMode", typeof(bool), typeof(SlideshowControl), new PropertyMetadata(true));
        public bool InCarouselMode
        {
            get { return (bool)GetValue(InCarouselModeProperty); }
            set { SetValue(InCarouselModeProperty, value); }
        }

        public SlideshowControl()
        {
            this.DefaultStyleKey = typeof(SlideshowControl);
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            this.SelectedItem = this.Items[this.SelectedIndex];

            //Button prevButton = this.GetTemplateChild("PreviousButton") as Button;
            //if (prevButton != null)
            //{
            //    prevButton.Click += new System.Windows.RoutedEventHandler(prevButton_Click);
            //}

            //Button nextButton = this.GetTemplateChild("NextButton") as Button;
            //if (nextButton != null)
            //{
            //    nextButton.Click += new System.Windows.RoutedEventHandler(nextButton_Click);
            //}
        }

        //private void prevButton_Click(object sender, System.Windows.RoutedEventArgs e)
        //{
        //    this.Reversed = this.InCarouselMode;
        //    this.SelectedIndex = (this.SelectedIndex - 1 + this.Items.Count) % (this.Items.Count);
        //    this.SelectedItem = this.Items[this.SelectedIndex];
        //}

        //private void nextButton_Click(object sender, System.Windows.RoutedEventArgs e)
        //{
        //    this.Reversed = false;
        //    this.SelectedIndex = (this.SelectedIndex + 1) % (this.Items.Count);
        //    this.SelectedItem = this.Items[this.SelectedIndex];
        //}

        public void OnPreviousClick()
        {
            this.SelectedIndex = (this.SelectedIndex - 1) % (this.Items.Count);
            this.SelectedItem = this.Items[this.SelectedIndex];
        }

        public void OnNextClick()
        {
            this.SelectedIndex = (this.SelectedIndex + 1) % (this.Items.Count);
            this.SelectedItem = this.Items[this.SelectedIndex];
        }
    }
}
